﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Resources;
using System.Reflection;
using System.Drawing;
using Resources;

namespace HRProject.Code.Common
{
    public static class ContentProvider
    {
        private static Dictionary<string, ResourceManager> labelResources;
        private static Dictionary<string, ResourceManager> valueResources;
        private static Dictionary<string, ResourceManager> messageResources;
        private static Dictionary<string, ResourceManager> formatResources;

        private static string[] supportLanguages;
        private static string defaultLanguage;
        public static string CurrentLanguage;
        public static int PageSize;

        static ContentProvider()
        {
            labelResources = new Dictionary<string, ResourceManager>();
            valueResources = new Dictionary<string, ResourceManager>();
            messageResources = new Dictionary<string, ResourceManager>();
            formatResources = new Dictionary<string, ResourceManager>();
            supportLanguages = new string[] { "en", "ja", "vi" };
            defaultLanguage = "vi";
            labelResources.Add("en", EnLabels.ResourceManager);
            labelResources.Add("ja", JaLabels.ResourceManager);
            labelResources.Add("vi", ViLabels.ResourceManager);
            valueResources.Add("en", EnValues.ResourceManager);
            valueResources.Add("ja", JaValues.ResourceManager);
            valueResources.Add("vi", ViValues.ResourceManager);
            messageResources.Add("en", EnMessages.ResourceManager);
            messageResources.Add("ja", JaMessages.ResourceManager);
            messageResources.Add("vi", ViMessages.ResourceManager);
            formatResources.Add("en", EnFormats.ResourceManager);
            formatResources.Add("ja", JaFormats.ResourceManager);
            formatResources.Add("vi", ViFormats.ResourceManager);
            CurrentLanguage = defaultLanguage;
            PageSize = 10;
        }

        private static string Get(Dictionary<string, ResourceManager> resources, string locale, string key)
        {
            string lang = locale;
            if (!supportLanguages.Contains(locale))
            {
                lang = defaultLanguage;
            }
            if (!resources.ContainsKey(lang))
            {
                return string.Empty;
            }
            string value = resources[lang].GetString(key);
            if (value != null)
            {
                return value;
            }
            value = resources[defaultLanguage].GetString(key);
            return (value != null) ? value : string.Empty;
        }

        public static string GetString(string locale, string key)
        {
            return Get(labelResources, locale, key);
        }

        public static string GetString(string key)
        {
            return GetString(CurrentLanguage, key);
        }

        public static string GetValue(string locale, string key)
        {
            return Get(valueResources, locale, key);
        }

        public static string GetValue(string key)
        {
            return GetValue(CurrentLanguage, key);
        }

        public static string GetMessage(string locale, string key)
        {
            return Get(messageResources, locale, key);
        }

        public static string GetMessage(string key)
        {
            return GetMessage(CurrentLanguage, key);
        }

        public static string GetFormat(string locale, string key)
        {
            return Get(formatResources, locale, key);
        }

        public static string GetFormat(string key)
        {
            return GetFormat(CurrentLanguage, key);
        }

        public static Bitmap GetImage(string locale, string key)
        {
            string lang = locale;
            if (!supportLanguages.Contains(locale))
            {
                lang = defaultLanguage;
            }
            if (!labelResources.ContainsKey(lang))
            {
                return null;
            }
            object value = (object)labelResources[lang].GetObject(key);
            if ((value != null) && typeof(Bitmap).Equals(value.GetType()))
            {
                return (Bitmap)value;
            }
            value = labelResources[defaultLanguage].GetObject(key);
            if ((value != null) && typeof(Bitmap).Equals(value.GetType()))
            {
                return (Bitmap)value;
            }
            return null;
        }
    }
}